package com.blit.lp.jf.controller;

import java.util.List;




import com.blit.lp.bus.security.Audit;
import com.blit.lp.core.context.User;
import com.blit.lp.core.exception.SysException;
import com.blit.lp.jf.config.LPController;
import com.blit.lp.tools.AuditLogKit;
import com.blit.lp.tools.LPCacheKit;
import com.blit.lp.tools.LPLogKit;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.ext.LPJsonRender;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;


/**
 * 登录和首面控制器
 * @author dkomj
 *
 */
@LPController(controllerkey="/")
public class IndexController extends Controller {

	public void index(){
		renderFreeMarker("/sys/index.html");
	}
	
	
	@Clear
	public void login(){
		renderFreeMarker("/sys/login.html");
	}
	
	//密码登录处理逻辑
	@Clear
	public void dologin(){
		try
		{
			String usernum = getPara("usernum");
			String pwd = getPara("pwd");
			
			String clientIp = AuditLogKit.getRealIp(getRequest());
			Audit.checkLimitIp(clientIp);

			User.doLogin(usernum, pwd);
			renderJson(Ret.ok());
		}
		catch(SysException e){
			e.printStackTrace();
			e.renderFreeMarker(this);
		}
		catch (Exception e) {
			LPLogKit.error("系统发生未知错误",e);
			setAttr("errmsg", "系统发生未知错误!");
			renderFreeMarker("/sys/err.html");
		}
		
	}
	
	//获取菜单树
	public void getOutLookTree(){
		User user = User.getCurrUser();
		String sql = "";
		if(user.isSuperAdmin()){
			sql = "select m.id,m.pid,menuname as text,url,openway from lp_sys_menu m order by px asc,addtime asc";
			List<Record> list = Db.find(sql);
			renderJson(new LPJsonRender(list));
		}
		else{
			sql = "select m.id,m.pid,menuname as text,url,openway from lp_sys_menu m" +
				" join lp_sys_rolemenu rm on m.id=rm.menuid" +
				" join lp_sys_role r on r.id=rm.roleid" +
				" join lp_sys_userrole ur on r.id=ur.roleid" +
				" where ur.userid=? order by px asc,addtime asc";
			List<Record> list = Db.find(sql,user.get("id"));
			renderJson(new LPJsonRender(list));
		}
	}
	
	//退出登录逻辑
	@Clear
	public void dologout(){
		User.doLogout();
		renderJson(Ret.ok());
	}
	
	//修改密码
	public void dorestpwd(){
		String pwd1 = getPara("pwd1");
		String pwd2 = getPara("pwd2");
		
		if(StrKit.isBlank("pwd1") || StrKit.isBlank("pwd2")){
			renderJson(Ret.fail("msg", "原、新密码不能为空!"));
			return;
		}
		
		User user = User.getCurrUser();
		
		String dbPwd = user.get("pwd");
		if(!dbPwd.equalsIgnoreCase(pwd1)){
			renderJson(Ret.fail("msg", "原密码错误！"));
			return;
		}
		
		Db.update("update lp_sys_user set pwd=? where id=?",pwd2,user.get("id"));
		renderJson(Ret.ok("msg", ""));
	}
	
	@Clear
	public void setSessionTest(){
//		getSession().setAttribute("omj_test", "omj_test");
//		renderJson(new LPJsonRender(Ret.ok("service","lp1").set("data", getSessionAttr("omj_test")));
		
		LPCacheKit.put("testcache", "omj_test", "omj_test");
		renderJson(Ret.ok("service","lp1").set("data", LPCacheKit.get("testcache", "omj_test")));
	}
	
	@Clear
	public void getSessionTest() {
		//renderJson(new LPJsonRender(Ret.ok("service","lp1").set("data", getSessionAttr("omj_test")));
		renderJson(Ret.ok("service","lp1").set("data", LPCacheKit.get("testcache", "omj_test")));
	}
}
